package com.xjscrm.console.service.customer.group.impl;

import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.mapper.WwGroupMapper;
import com.xjscrm.console.param.group.WwGroupQuery;
import com.xjscrm.console.param.group.WwGroupTagRelDto;
import com.xjscrm.console.service.customer.group.WwGroupService;
import com.xjscrm.console.service.customer.group.WwGroupTagBatchService;
import com.xjscrm.console.service.customer.group.WwGroupTagService;
import com.xjscrm.console.vo.WwGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WwGroupTagBatchServiceImpl implements WwGroupTagBatchService {

    @Autowired
    private WwGroupTagService wwGroupTagService;

    @Autowired
    private WwGroupMapper wwGroupMapper;

    @Autowired
    private WwGroupService wwGroupService;
    @Override
    public void BatchAddWwGroupTagRel(WwGroupTagRelDto wwGroupTagRelDto) {
      if(wwGroupTagRelDto.getIds()==null||wwGroupTagRelDto.getIds().isEmpty()){
          throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
      }
        if(wwGroupTagRelDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }

        if(wwGroupTagRelDto.getSelectType()==0){
            //通过主键ID查询 群微信ID
            WwGroupQuery wwGroupQuery=new WwGroupQuery();
            wwGroupQuery.setMerId(wwGroupTagRelDto.getMerId());
            wwGroupQuery.setIds(wwGroupTagRelDto.getIds());
            List<WwGroupVo> wwGroupList = wwGroupMapper.getWwGroupList(wwGroupQuery);

            if(wwGroupList==null||wwGroupList.isEmpty()){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "未找到客户群");
            }

            for (WwGroupVo wwGroupVo:wwGroupList) {
                try {
                    wwGroupTagRelDto.setGroupWxid(wwGroupVo.getGroupWxid());
                    wwGroupTagRelDto.setDeviceId(wwGroupVo.getDeviceId());
                    wwGroupTagService.AddWwGroupTagRel(wwGroupTagRelDto);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        if(wwGroupTagRelDto.getSelectType()==1){
            addWwGroupTagRelBySelect(wwGroupTagRelDto);
        }

    }

    @Async("taskExecutor")
    public void addWwGroupTagRelBySelect(WwGroupTagRelDto wwGroupTagRelDto) {
        if(wwGroupTagRelDto.getWwGroupQuery()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.PARAMETER_ERROR);
        }
        WwGroupQuery wwGroupQuery = wwGroupTagRelDto.getWwGroupQuery();
        Long idStart = 0L;
        wwGroupQuery.setMerId(wwGroupTagRelDto.getMerId());
        wwGroupQuery.setAsc(true);
        wwGroupQuery.setCurrent(1);
        wwGroupQuery.setSize(100);
        wwGroupQuery.setOrderByField("id");
        wwGroupQuery.setIdStart(idStart);
        List<WwGroupVo> records = wwGroupService.getWwGroupList(wwGroupQuery).getRecords();

        while (records != null && !records.isEmpty()) {
            for (WwGroupVo wwGroupVo : records) {
                try {
                    if (wwGroupVo.getId().longValue() > idStart.longValue()) {
                        idStart = wwGroupVo.getId();
                    }
                    wwGroupTagRelDto.setGroupWxid(wwGroupVo.getGroupWxid());
                    wwGroupTagRelDto.setDeviceId(wwGroupVo.getDeviceId());
                    wwGroupTagService.AddWwGroupTagRel(wwGroupTagRelDto);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            wwGroupQuery.setIdStart(idStart);
            records = wwGroupService.getWwGroupList(wwGroupQuery).getRecords();
        }

    }

    @Override
    public void BatchDeleWwGroupTagRel(WwGroupTagRelDto wwGroupTagRelDto) {
//        if(wwGroupTagRelDto.getIds()==null||wwGroupTagRelDto.getIds().isEmpty()){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
//        }
        if(wwGroupTagRelDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }

        if(wwGroupTagRelDto.getSelectType()==0){
            WwGroupQuery wwGroupQuery=new WwGroupQuery();
            wwGroupQuery.setMerId(wwGroupTagRelDto.getMerId());
            wwGroupQuery.setIds(wwGroupTagRelDto.getIds());
            List<WwGroupVo> wwGroupList = wwGroupMapper.getWwGroupList(wwGroupQuery);

            if(wwGroupList==null||wwGroupList.isEmpty()){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "未找到客户群");
            }

            for (WwGroupVo wwGroupVo:wwGroupList) {
                try {
                    wwGroupTagRelDto.setGroupWxid(wwGroupVo.getGroupWxid());
                    wwGroupTagRelDto.setDeviceId(wwGroupVo.getDeviceId());
                    wwGroupTagService.deleWwGroupTagRel(wwGroupTagRelDto);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        if(wwGroupTagRelDto.getSelectType()==1){
            deleWwGroupTagRelBySelect(wwGroupTagRelDto);
        }

    }

    @Async("taskExecutor")
    public void deleWwGroupTagRelBySelect(WwGroupTagRelDto wwGroupTagRelDto) {
        if(wwGroupTagRelDto.getWwGroupQuery()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.PARAMETER_ERROR);
        }
        WwGroupQuery wwGroupQuery = wwGroupTagRelDto.getWwGroupQuery();
        Long idStart = 0L;
        wwGroupQuery.setMerId(wwGroupTagRelDto.getMerId());
        wwGroupQuery.setAsc(true);
        wwGroupQuery.setCurrent(1);
        wwGroupQuery.setSize(100);
        wwGroupQuery.setOrderByField("id");
        wwGroupQuery.setIdStart(idStart);
        List<WwGroupVo> records = wwGroupService.getWwGroupList(wwGroupQuery).getRecords();

        while (records != null && !records.isEmpty()) {
            for (WwGroupVo wwGroupVo : records) {
                try {
                    if (wwGroupVo.getId().longValue() > idStart.longValue()) {
                        idStart = wwGroupVo.getId();
                    }
                    wwGroupTagRelDto.setGroupWxid(wwGroupVo.getGroupWxid());
                    wwGroupTagRelDto.setDeviceId(wwGroupVo.getDeviceId());
                    wwGroupTagService.deleWwGroupTagRel(wwGroupTagRelDto);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            wwGroupQuery.setIdStart(idStart);
            records = wwGroupService.getWwGroupList(wwGroupQuery).getRecords();
        }
    }

    @Override
    public void BatchMoveWwGroupTagRel(WwGroupTagRelDto wwGroupTagRelDto) {
        if(wwGroupTagRelDto.getIds()==null||wwGroupTagRelDto.getIds().isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(wwGroupTagRelDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }

        if(wwGroupTagRelDto.getSelectType()==0){
            WwGroupQuery wwGroupQuery=new WwGroupQuery();
            wwGroupQuery.setMerId(wwGroupTagRelDto.getMerId());
            wwGroupQuery.setIds(wwGroupTagRelDto.getIds());
            List<WwGroupVo> wwGroupList = wwGroupMapper.getWwGroupList(wwGroupQuery);

            for (WwGroupVo wwGroupVo:wwGroupList) {
                try {
                    wwGroupTagRelDto.setGroupWxid(wwGroupVo.getGroupWxid());
                    wwGroupTagRelDto.setDeviceId(wwGroupVo.getDeviceId());
                    wwGroupTagService.moveWwGroupTagRel(wwGroupTagRelDto);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        if(wwGroupTagRelDto.getSelectType()==1){
            moveWwGroupTagRelBySelect(wwGroupTagRelDto);
        }


    }

    @Async("taskExecutor")
    public void moveWwGroupTagRelBySelect(WwGroupTagRelDto wwGroupTagRelDto) {
        if(wwGroupTagRelDto.getWwGroupQuery()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.PARAMETER_ERROR);
        }
        WwGroupQuery wwGroupQuery = wwGroupTagRelDto.getWwGroupQuery();
        Long idStart = 0L;
        wwGroupQuery.setMerId(wwGroupTagRelDto.getMerId());
        wwGroupQuery.setAsc(true);
        wwGroupQuery.setCurrent(1);
        wwGroupQuery.setSize(100);
        wwGroupQuery.setOrderByField("id");
        wwGroupQuery.setIdStart(idStart);
        List<WwGroupVo> records = wwGroupService.getWwGroupList(wwGroupQuery).getRecords();

        while (records != null && !records.isEmpty()) {
            for (WwGroupVo wwGroupVo : records) {
                try {
                    if (wwGroupVo.getId().longValue() > idStart.longValue()) {
                        idStart = wwGroupVo.getId();
                    }
                    wwGroupTagRelDto.setGroupWxid(wwGroupVo.getGroupWxid());
                    wwGroupTagRelDto.setDeviceId(wwGroupVo.getDeviceId());
                    wwGroupTagService.moveWwGroupTagRel(wwGroupTagRelDto);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            wwGroupQuery.setIdStart(idStart);
            records = wwGroupService.getWwGroupList(wwGroupQuery).getRecords();
        }
    }
}
